<?php
	/**
	 * @package AmpleHarvest
	 */
	namespace ampleharvest\code;
	use\ampleharvest\code\cConfig;

	require_once dirname(__DIR__) . '/config/cConfig.php';
	/**
	 * @brief This class holds all the Locations for Rest Services used
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 02 SEP 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code config classes
	 */
	if (!class_exists("ampleharvest\code\cRestConnector"))
	{
		class cRestConnector
		{
			protected $m_arr_Param = array();
			protected $m_str_Url = "";
			protected $m_str_UserName = "";
			protected $m_str_Password = "";
			protected $m_arr_HttpHeaders = array();

			protected $m_arr_Options = array();

			protected $m_str_LastError = "";
			protected $m_int_Code = "";

			public function __construct($str_URL)
			{
				$this->m_str_Url = $str_URL;

				$this->m_arr_HttpHeaders["Accept"] = "application/json";
				$this->m_arr_HttpHeaders["Content-type"] = "text/xml";
				$this->m_arr_HttpHeaders["charset"] = "utf-8";

				if (cConfig::C_STR_SSL_CAINFO != "")
				{
					$this->m_arr_Options[CURLOPT_CAINFO] = cConfig::C_STR_SSL_CAINFO;
				}
				if (cConfig::C_STR_SSL_CERT != "")
				{
					$this->m_arr_Options[CURLOPT_SSLCERT] = cConfig::C_STR_SSL_CERT;
				}

				if (cConfig::C_STR_SSL_KEY != "")
				{
					$this->m_arr_Options[CURLOPT_SSLKEY] = cConfig::C_STR_SSL_KEY;
				}
			}

			public function setOptions($arr_Options)
			{
				if (!is_array($arr_Options))
				{
					return false;
				}

				foreach ($arr_Options as $str_Key => $str_Option)
				{
					$this->m_arr_Options[$str_Key] = $str_Option;
				}
			}

			public function getLastError()
			{
				return $this->m_str_LastError;
			}

			public function getCode()
			{
				return $this->m_int_Code;
			}

			public function clear_Params()
			{
				$this->m_arr_Param = array();
			}

			public function set_Url($str_URL)
			{
				$this->m_str_Url = $str_URL;
			}

			public function set_HttpHeaders($str_Name, $str_Value)
			{
				$this->m_arr_HttpHeaders[$str_Name] = $str_Value;
			}

			public function set_Param($str_Name, $str_Value)
			{
				$this->m_arr_Param[$str_Name] = $str_Value;
			}

			public function set_Login($str_UserName, $str_Password)
			{
				$this->m_str_UserName = $str_UserName;
				$this->m_str_Password = $str_Password;
			}

			public function send_Put()
			{
				if (!$this->m_str_Url)
				{
					return false;
				}

				$arr_Headers = array();
				foreach ($this->m_arr_HttpHeaders as $str_Key => $str_Value)
				{
					$arr_Headers[] = $str_Key . ": " . $str_Value;
				}

				$obj_Curl = curl_init($this->m_str_Url);

				//print("\n-".$this->m_str_Url."-\n");

				//print($str_URL."\n");

				curl_setopt($obj_Curl, CURLOPT_HTTPHEADER, $arr_Headers);
				if ($this->m_str_UserName)
				{
					curl_setopt($obj_Curl, CURLOPT_USERPWD, $this->m_str_UserName . ":" . $this->m_str_Password);
				}

				if ($this->m_arr_Options)
				{
					curl_setopt_array($obj_Curl, $this->m_arr_Options);
				}

				curl_setopt($obj_Curl, CURLOPT_RETURNTRANSFER, true);

				curl_setopt($obj_Curl, CURLOPT_CUSTOMREQUEST, "PUT");
				curl_setopt($obj_Curl, CURLOPT_POSTFIELDS, http_build_query($this->m_arr_Param));

				//print_r($this->m_arr_Param);

				$this->m_int_Code = curl_getinfo($obj_Curl, CURLINFO_HTTP_CODE);

				//print("\n-".$this->m_int_Code."-\n");

				$str_Return = curl_exec($obj_Curl);

				$this->m_str_LastError = curl_getinfo($obj_Curl, CURLINFO_HEADER_OUT);

				//print("\n-".$this->m_str_LastError."-\n");
				curl_close($obj_Curl);

				if (!$str_Return)
				{
					return false;
				}
				return $str_Return;
			}

			public function send_Post()
			{
				if (!$this->m_str_Url)
				{
					return false;
				}

				$arr_Headers = array();
				foreach ($this->m_arr_HttpHeaders as $str_Key => $str_Value)
				{
					$arr_Headers[] = $str_Key . ": " . $str_Value;
				}

				$obj_Curl = curl_init($this->m_str_Url);

				//print("\n-".$this->m_str_Url."-\n");

				//print($str_URL."\n");

				curl_setopt($obj_Curl, CURLOPT_HTTPHEADER, $arr_Headers);
				if ($this->m_str_UserName)
				{
					curl_setopt($obj_Curl, CURLOPT_USERPWD, $this->m_str_UserName . ":" . $this->m_str_Password);
				}

				if ($this->m_arr_Options)
				{
					curl_setopt_array($obj_Curl, $this->m_arr_Options);
				}

				curl_setopt($obj_Curl, CURLOPT_RETURNTRANSFER, true);

				curl_setopt($obj_Curl, CURLOPT_CUSTOMREQUEST, "POST");
				curl_setopt($obj_Curl, CURLOPT_POSTFIELDS, http_build_query($this->m_arr_Param));

				//print_r($this->m_arr_Param);

				$this->m_int_Code = curl_getinfo($obj_Curl, CURLINFO_HTTP_CODE);

				//print("\n-".$this->m_int_Code."-\n");

				$str_Return = curl_exec($obj_Curl);

				$this->m_str_LastError = curl_getinfo($obj_Curl, CURLINFO_HEADER_OUT);

				//print("\n-".$this->m_str_LastError."-\n");
				curl_close($obj_Curl);

				if (!$str_Return)
				{
					return false;
				}
				return $str_Return;
			}

			public function send_Get()
			{
				if (!$this->m_str_Url)
				{
					return false;
				}

				$obj_Curl = \curl_init();

				\curl_setopt($obj_Curl, CURLOPT_URL, $this->m_str_Url);
				\curl_setopt($obj_Curl, CURLOPT_RETURNTRANSFER, 1);
				if ($this->m_arr_Options)
				{
					curl_setopt_array($obj_Curl, $this->m_arr_Options);
				}

				$str_Return = \curl_exec($obj_Curl);

				$this->m_int_Code = curl_getinfo($obj_Curl, CURLINFO_HTTP_CODE);

				\curl_close($obj_Curl);
				if ($this->m_int_Code != 200)
				{
					$this->m_str_LastError = $str_Return;
					return false;
				}

				return $str_Return;
			}
		}
	}
